package stack.era.database;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public abstract class Saver<T> {

	private File saveFile;

	public Saver(String fileName) {
		saveFile = new File("database/" + fileName + ".era");
	}

	public void saveObjects(ArrayList<T> list) {
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
					saveFile));
			oos.writeObject(list);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public ArrayList<T> loadObjects() {
		try {
			ObjectInputStream oos = new ObjectInputStream(new FileInputStream(
					saveFile));
			return (ArrayList<T>) oos.readObject();
		} catch (FileNotFoundException e) {
			// Game failed to load
		} catch (IOException e) {
			// Game failed to load
		} catch (ClassNotFoundException e) {
			// Game failed to load
		}
		return null;
	}

}
